package com.bwie.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.bwie.mapper.CustomerMapper;
import com.bwie.pojo.Result;
import com.bwie.pojo.dto.CustomerDto;
import com.bwie.pojo.po.Customer;
import com.bwie.service.CustomerService;
import com.bwie.utils.poi.EasyPoiUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @RequestMapping("toCustomerList")
    public String toCustomerList(){
        return "list";
    }

    @Autowired
    public CustomerService customerService;
    @Autowired
    public CustomerMapper customerMapper;
    @Autowired
    public EasyPoiUtil easyPoiUtil;

    @GetMapping("customerList")
    @ResponseBody
    public PageInfo<Customer>customerList(CustomerDto customerDto){
        return customerService.customerList(customerDto);
    }

    @PostMapping("insterFile")
    @ResponseBody
    public Result insterFile(@RequestParam("file") MultipartFile file) throws Exception {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        List<Customer> list = ExcelImportUtil.importExcel(file.getInputStream(), Customer.class, importParams);
        customerMapper.add(list);
        return Result.success("上传成功");
    }

    @RequestMapping("daochu")
    @ResponseBody
    public void daochu(HttpServletResponse response,@RequestParam String customerName){
        List<Customer> customerList= customerMapper.selectCustomerName(customerName);
        easyPoiUtil.exportExcel(response,customerList,Customer.class,"哈哈哈");
    }

    @RequestMapping("xuandao")
    @ResponseBody
    public void xuandao(HttpServletResponse response,Customer customer){
        List<Customer> customerList= customerMapper.selectCustomerId(customer.getIds());
        easyPoiUtil.exportExcel(response,customerList,Customer.class,"嘻嘻");
    }
}
